import { defineStore } from "pinia";
import { ref } from "vue";
import { Account, Auth, LoginLog } from "../apis/account/type";
import { login, wechatLogin } from "../apis/account/account";

export const useAuthSotre= defineStore("user",()=>{

    const token=ref("");
    const account=ref<Account>({});
    const loginLog=ref<LoginLog>({});

    const loginWechatApi=async (code:string)=>{


      const res=await wechatLogin(code);
 
      if(res.token){

        token.value=res.token;
        account.value=res.model;
        loginLog.value=res.loginLog;

      }

      return res;

     
 
    }
    

    const loginApi=async (data:Auth)=>{

      const res=await login(data).catch(()=>undefined);
      if(res){

        token.value=res.token;
        account.value=res.model;
        loginLog.value=res.loginLog;

      }

      return res;

     
 
    }

    return{
        token,account,loginLog,loginApi,loginWechatApi
    }


},{
  persist:{
       storage: localStorage,
   }
}


)